<?php
require_once(dirname(__FILE__) . '/include/config.inc.php');


$dosql->Execute("SELECT * FROM  `fa_cms_archives`  WHERE  id>1800 and model_id=3 and description=''  ", 8);
while ($row3 = $dosql->GetArray(8)) {
    $id=$row3['id'];
    $tbid=$row3['tbid'];
    echo $tbid;
    if ($tbid>0) {
        $url = "https://sgf.zishaq.com/web/index.php?store_id=1&r=api/default/goods&id=".$tbid."&access_token=X4UKUQEO6ZtN1T2ZvkymOG1ZR6oDCBOD&_version=2.8.9&_platform=wx";
        $res = httpGet($url);
        $res=json_decode($res,true);

        if(is_array($res)){
/*            $pic = $res['data']['pic_list'][0]['pic_url'];
            if($pic!=""){
                $newpic="/".GetRemPic($pic);
                $dosql->Execute("update  `fa_cms_archives`  set  image='$newpic' where id='$id' ");

            }*/

            $desc=$res['data']['goods_brief'];
            $dosql->Execute("update  `fa_cms_archives`  set  description='$desc' where id='$id' ");


        }


        //$dosql->Execute("update  `fa_cms_archives`  set  image='$pic_arr[0]' where id='$id' ");


        echo $id."更新成功<br>";
    }

}



function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    //curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');//这是根据http://curl.haxx.se/ca/cacert.pem 下载的证书，添加这句话之后就运行正常了
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);

    curl_close($curl);

    return $res;
}



//GetRemPic("https://img.baocangzisha.com/web/uploads/image/store_1/52192480f2152860164cab9be2c834ca36349055.jpg");

/*
 * 获取一个远程图片
 *
 * @access  public
 * @param   $url       string  获取字段所属模型
 * @return  $save_url  string  返回上传后地址
*/
function GetRemPic($url)
{

    global $cfg_image_dir;

    //引入下载类
    require_once(PHPMYWIND_DATA.'/httpfile/down.class.php');

    //初始化变量
    $url = stripslashes($url);
    $host = 'https://'.$_SERVER['HTTP_HOST'];

    //初始化下载类
    $htd = new HttpDown();
    $htd->OpenUrl($url);



    $type = $htd->GetHead('content-type');


    if($type == 'image/gif')
        $tempfile_ext = 'gif';

    else if($type == 'image/png')
        $tempfile_ext = 'png';

    else if($type == 'image/wbmp')
        $tempfile_ext = 'bmp';

    else
        $tempfile_ext = 'jpg';


    $upload_url = 'image';
    $upload_dir = $cfg_image_dir;

    $ymd = date('Ymd');
    $upload_url .= '/'.$ymd;
    $upload_dir .= '/'.$ymd;

    if(!file_exists($upload_dir))
    {
        mkdir($upload_dir);

        $fp = fopen($upload_dir.'/index.htm', 'w');
        fclose($fp);
    }

    //上传文件名称
    $filename = time().'_'.rand(1,9999).'.'.$tempfile_ext;

    //上传文件路径
    $save_url = 'uploads/'.$upload_url.'/'.$filename;

    //生成本地路径
    $self = explode('/', $_SERVER['PHP_SELF']);
    $self_size = count($self) - 2;
    $self_str  = '';
    for($i=0; $i<$self_size; $i++)
    {
        $self_str .= $self[$i].'/';
    }

    $save_url = $self_str.'uploads/'.$upload_url.'/'.$filename;
    $save_dir = $upload_dir.'/'.$filename;

    $rs = $htd->SaveToBin($save_dir);

    $htd->Close();
    return ($rs ? $save_url : '');
}



//获得文章内容里的外部资源
function GetContFile($body)
{

    global $cfg_image_dir;

    //引入下载类
    require_once(PHPMYWIND_DATA.'/httpfile/down.class.php');

    //初始化变量
    $body = stripslashes($body);
    $host = 'https://'.$_SERVER['HTTP_HOST'];

    //过滤图片文件
    $pic_arr = array();
    preg_match_all("/src=[\"|'|\s]{0,}(https:\/\/([^>]*)\.(gif|jpg|png|bmp))/isU", $body, $pic_arr);
    $pic_arr = array_unique($pic_arr[1]);

    //var_dump($pic_arr);

    //初始化下载类
    $htd = new HttpDown();

    foreach($pic_arr as $k=>$v)
    {

        //$v=urldecode($v);
        if(preg_match('#'.$host.'#i', $v)) continue;
        if(!preg_match('#^https:\/\/#i', $v)) continue;


        $htd->OpenUrl($v);



        $type = $htd->GetHead('content-type');


        if($type == 'image/gif')
            $tempfile_ext = 'gif';

        else if($type == 'image/png')
            $tempfile_ext = 'png';

        else if($type == 'image/wbmp')
            $tempfile_ext = 'bmp';

        else
            $tempfile_ext = 'jpg';


        $upload_url = 'image';
        $upload_dir = $cfg_image_dir;

        $ymd = date('Ymd');
        $upload_url .= '/'.$ymd;
        $upload_dir .= '/'.$ymd;

        if(!file_exists($upload_dir))
        {
            mkdir($upload_dir);

            $fp = fopen($upload_dir.'/index.htm', 'w');
            fclose($fp);
        }

        //上传文件名称
        $filename = time().'_'.rand(1,9999).'.'.$tempfile_ext;

        //上传文件路径
        $save_url = 'uploads/'.$upload_url.'/'.$filename;

        //生成本地路径
        $self = explode('/', $_SERVER['PHP_SELF']);
        $self_size = count($self) - 2;
        $self_str  = '';
        for($i=0; $i<$self_size; $i++)
        {
            $self_str .= $self[$i].'/';
        }

        $save_url = $self_str.'uploads/'.$upload_url.'/'.$filename;
        $save_dir = $upload_dir.'/'.$filename;

        $rs = $htd->SaveToBin($save_dir);
        if($rs)
        {
            $body = str_replace(trim($v), $save_url, $body);
        }
    }

    $htd->Close();


    //回传转义字符串
    return _RunMagicQuotes($body);
}


?>